package com.corner.ui.adapter;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.corner.R;
import com.corner.app.App;
import com.corner.app.ConsTants;
import com.corner.app.GlobalParams;
import com.corner.model.cornerservice.domain.Info;
import com.corner.model.cornerservice.domain.User;
import com.corner.presenter.imp.InfoBasePresenter;
import com.corner.ui.activity.ChannelActivity;
import com.corner.ui.activity.ChatActivity;
import com.corner.ui.activity.IntersectionActivity;
import com.corner.ui.activity.ProfileActivity;
import com.corner.ui.activity.SelectContactActivity;
import com.corner.ui.activity.ShowImageActivity;
import com.corner.ui.base.BaseActivity;
import com.corner.ui.base.BaseMVPAdapter;
import com.corner.ui.view.IInfoBaseView;
import com.corner.utils.AudioPlayer;
import com.corner.utils.DateFormatUtil;
import com.corner.utils.DialogUtil;
import com.corner.utils.DownLoadUtil;
import com.corner.utils.ToastUtil;
import com.corner.utils.view.CustomFont;
import com.corner.utils.view.FindView;
import com.corner.utils.view.InjectViewUtils;
import com.corner.utils.view.SquareImageView;
import com.facebook.drawee.view.SimpleDraweeView;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * Created by Administrator on 2015/5/21.
 */
public class InfoAdapter extends BaseMVPAdapter<InfoBasePresenter> implements View.OnClickListener, IInfoBaseView {
    public static final String AUDIO_PATH = "/audio/";
    /**
     * ImageView对应的状态
     */
    public static final int iv_type_audio = 1;
    public static final int iv_type_delete = 2;
    public static final int iv_type_like = 3;
    public static final int iv_type_chat = 4;
    public static final int iv_type_avater = 5;
    private static final int SHOW_PROGRESS = 9;
    private static final int TP = 0;
    private static final int AP = 1;
    private static final int A = 2;
    private boolean isProFile;
    private List<Info> infos;
    private Context context;
    /**
     * 删除对话框
     */
    private Dialog deleteDialog;
    private TextView tvEnsure;
    private TextView tvCancel;

    /**
     * 更多对话框
     */
    private Dialog moreDeleteDialog;
    private Dialog moreNoWantSeeDialog;
    private TextView tvNotWantSee;
    private TextView tvDelete;
    private TextView tvOtherShare;
    private TextView tvShareMine;
    private Dialog reportDialog;
    private TextView tvReportCancel;
    private TextView tvReortEnsure;
    private TextView tvOtherReport;
    private AudioPlayer audioPlayer;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case SHOW_PROGRESS:
                    AudioProgress audioProgress = (AudioProgress) msg.obj;
                    if (audioProgress.tvProgress.getTag() == audioProgress.info) {

                        if (getAudioPath(audioProgress.info).equals(audioPlayer.getPath())) {
                            int curr = audioPlayer.getCurrPosition();
                            int duration = audioPlayer.getDuration();
                            if (!(curr == 0 && duration == 0)) {
                                audioProgress.info.marginRight = (int) ((int) (audioProgress.tvProgress.getTag(R.integer.info_a_tv_progress_first_width)) * (1 - (curr * 1f / duration)));
                                changeMargin(audioProgress.info, audioProgress.tvProgress);
                                if (curr != duration) {
                                    InfoAdapter.this.sendMessage(audioProgress.info, audioProgress.tvProgress);
                                }
                            }
                        }
                    }
                    break;
            }
        }
    };

    public InfoAdapter(List<Info> infos, Context context, boolean isProFile) {
        this.infos = infos;
        this.context = context;
        this.isProFile = isProFile;
    }

    @Override
    public int getCount() {
        return infos.size();
    }

    @Override
    public Object getItem(int position) {
        return infos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            switch (getItemViewType(position)) {
                case TP:
                    convertView = View.inflate(App.application, R.layout.info_item_tp, null);
                    inflateTP(position, convertView);
                    break;
                case AP:
                    convertView = View.inflate(App.application, R.layout.info_item_ap, null);
                    inflateAP(position, convertView);
                    break;
                case A:
                    convertView = View.inflate(App.application, R.layout.info_item_a, null);
                    inflateA(position, convertView);
                    break;
            }
        }
        switch (getItemViewType(position)) {
            case TP:
                TP tp = (InfoAdapter.TP) convertView.getTag();
                fillTP(position, tp);
                break;
            case AP:
                AP ap = (InfoAdapter.AP) convertView.getTag();
                fillAP(position, ap);
                break;
            case A:
                A a = (InfoAdapter.A) convertView.getTag();
                fillA(position, a);
                break;
        }
        return convertView;
    }

    private void inflateA(int position, View convertView) {
        A a = new A();
        InjectViewUtils.injectView(a, convertView, App.application);
        InjectViewUtils.injectView(a.base, convertView, App.application);
        convertView.setTag(a);
        fillA(position, a);
    }

    private void fillA(int position, A a) {
        Info info = infos.get(position);
        a.ivAudio.setOnClickListener(this);
        a.ivAudio.setTag(info);
        a.ivAudio.setTag(R.integer.info_a_tv_progress, a.tvProgress);
        a.tvProgress.setTag(info);
        a.ivAudio.setTag(R.integer.info_iv_type, iv_type_audio);
        changeMargin(info, a.tvProgress);
        String path = getAudioPath(info);
        if (!new File(path).exists()) {
            DownLoadUtil.getInstance().getFilePath(path, info.getAudioUrl(), null);
        }
        changeAudioDrawable(info, a.ivAudio);
        inflateBase(info, a.base);
    }

    @Override
    public void showLoading(boolean isShow) {
        ((BaseActivity) context).showLoading(isShow);
    }

    /**
     * 改变音频Progress的margin
     *
     * @param info
     * @param tvProgress
     */
    private void changeMargin(Info info, TextView tvProgress) {
        if (info == tvProgress.getTag()) {
            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) tvProgress.getLayoutParams();
            layoutParams.rightMargin = info.marginRight;
            tvProgress.setLayoutParams(layoutParams);
        }
    }

    private String getAudioPath(Info info) {
        if (TextUtils.isEmpty(info.getAudioUrl()))
            return "";
        return GlobalParams.APPFIEPATH + AUDIO_PATH + info.getAudioUrl().hashCode() + ".amr";
    }

    private void inflateAP(int position, View convertView) {
        AP ap = new AP();
        InjectViewUtils.injectView(ap, convertView, App.application);
        InjectViewUtils.injectView(ap.base, convertView, App.application);
        convertView.setTag(ap);
        fillAP(position, ap);
    }

    private void fillAP(int position, AP ap) {
        Info info = infos.get(position);
        ap.viewPic.setScale(info.getScale());
        if (!TextUtils.isEmpty(info.getImageUrl()))
            ap.viewPic.setImageUri(Uri.parse(info.getImageUrl()));
        setImageFileUriIfHasNotSended(ap.viewPic, info);
        ap.viewPic.setOnClickListener(this);
        ap.viewPic.setTag(info);
        ap.viewPic.setTag(R.integer.info_ap_iv_audio, ap.ivAudio);
        ap.ivAudio.setTag(info);
        String path = getAudioPath(info);
        if (!new File(path).exists()) {
            DownLoadUtil.getInstance().getFilePath(path, info.getAudioUrl(), null);
        }
        if (!TextUtils.isEmpty(info.getText())) {
            ap.tvText.setVisibility(View.VISIBLE);
            ap.tvText.setText(info.getText());
        } else {
            ap.tvText.setVisibility(View.GONE);
        }
        changeAudioDrawable(info, ap.ivAudio);
        inflateBase(info, ap.base);
    }

    /**
     * 如果没有发送成功时，设置的图片Uri
     * @param view
     * @param info
     */
    private void setImageFileUriIfHasNotSended(SquareImageView view, Info info) {
        if (info.getObjectId()==null&&!TextUtils.isEmpty(info.getImagePath())){
            view.setImageUri(Uri.parse("file://" + info.getImagePath()));
        }
    }

    private void inflateTP(int position, View convertView) {
        TP tp = new TP();
        InjectViewUtils.injectView(tp, convertView, App.application);
        InjectViewUtils.injectView(tp.base, convertView, App.application);
        convertView.setTag(tp);
        fillTP(position, tp);
    }

    private void fillTP(int position, TP tp) {
        Info info = infos.get(position);
        switch (info.getType()) {
            case Info.T:
                tp.tvDivider.setVisibility(View.GONE);
                tp.tvText.setVisibility(View.VISIBLE);
                tp.viewPic.setVisibility(View.GONE);
                break;
            case Info.P:
                tp.tvText.setVisibility(View.GONE);
                tp.viewPic.setVisibility(View.VISIBLE);
                tp.tvDivider.setVisibility(View.VISIBLE);
                break;
            case Info.TP:
                tp.tvDivider.setVisibility(View.VISIBLE);
                tp.tvText.setVisibility(View.VISIBLE);
                tp.viewPic.setVisibility(View.VISIBLE);
                break;
        }
        tp.tvText.setText(info.getText());
        tp.viewPic.setScale(info.getScale());
        if (info.getType() != Info.T) {
            if (info.getImageUrl() != null) {
                tp.viewPic.setImageUri(Uri.parse(info.getImageUrl()));
            }
            setImageFileUriIfHasNotSended(tp.viewPic, info);
            tp.viewPic.setOnClickListener(this);
            tp.viewPic.setTag(info);
        }
        inflateBase(info, tp.base);
    }

    private void inflateBase(final Info info, final Base base) {
        boolean isSelf = info.getUser().getObjectId().equals(User.getCurrentUser().getObjectId());
        if (isSelf) {
            base.ibDelete.setVisibility(View.VISIBLE);
            base.ibChat.setVisibility(View.GONE);
        } else {
            base.ibDelete.setVisibility(View.GONE);
            base.ibChat.setVisibility(View.VISIBLE);
        }

        base.roundedImageView.setTag(info);
        base.roundedImageView.setTag(R.integer.info_iv_type, iv_type_avater);
        base.roundedImageView.setOnClickListener(this);

        if (info.getUser().getAvaterUrl() != null) {
            base.roundedImageView.setImageURI(Uri.parse(info.getUser().getAvaterUrl()));
        } else {
            base.roundedImageView.setImageURI(null);
        }

        if (!isProFile) {
            base.tvNickName.setText(info.getUser().getUsername());
        } else {
            if (info.getArea() != null) {
                base.tvNickName.setText(info.getArea().getLocationName());
                base.tvNickName.setOnClickListener(this);
                base.tvNickName.setTag(info);
                base.tvTime.setTag(info);
                base.tvTime.setOnClickListener(this);
            } else {
                base.tvNickName.setText(info.getUser().getUsername());
            }
        }
        base.tvTime.setText(DateFormatUtil.getShowTime(info.getCreateAt(), context));

        base.tvAgreeNum.setText("0");
        base.tvWatchNum.setText("0");
        base.tvAgreeNum.setOnClickListener(null);
        base.tvWatchNum.setOnClickListener(null);
        base.ibDelete.setOnClickListener(null);
        base.ibMore.setOnClickListener(null);
        base.ibLike.setImageResource(R.drawable.love_img);
        if (info.getObjectId()!=null) {

            //设置同感数量
            if (info.agreeNum != null) {
                base.tvAgreeNum.setText(info.agreeNum);
                base.tvAgreeNum.setTag(info);
                base.tvAgreeNum.setOnClickListener(InfoAdapter.this);
            } else if (info.agreeNum==null&&!info.hasRequestAgreeCount){
                base.tvAgreeNum.setTag(info);
                presenter.requestAgreeCount(base, info);
                info.hasRequestAgreeCount = true;
            }
            if (info.watchNum != null) {
                base.tvWatchNum.setText(info.watchNum);
                base.tvWatchNum.setTag(info);
                base.tvWatchNum.setOnClickListener(InfoAdapter.this);
            } else if (info.watchNum==null&&!info.hasRequestReadedCount){
                base.tvWatchNum.setTag(info);
                presenter.requestReadedCount(base, info);
                info.hasRequestReadedCount = true;
            }
            if (info.isLike != null) {
                initLike(info, base.ibLike);
                base.ibLike.setTag(info);
                base.ibLike.setTag(R.integer.info_base_ivlike_tag_tvagree, base.tvAgreeNum);
                base.ibLike.setTag(R.integer.info_iv_type, iv_type_like);
                base.ibLike.setOnClickListener(this);
            } else if (info.isLike == null && !info.hasRequestIsLike) {
                base.ibLike.setTag(info);
                presenter.requestIsLike(base, info);
                info.hasRequestIsLike = true;
            }

            base.ibDelete.setTag(info);
            base.ibDelete.setTag(R.integer.info_iv_type, iv_type_delete);
            base.ibDelete.setOnClickListener(this);

            base.ibMore.setTag(info);
            base.ibMore.setOnClickListener(this);

        }
        base.ibChat.setTag(info);
        base.ibChat.setTag(R.integer.info_iv_type, iv_type_chat);
        base.ibChat.setOnClickListener(this);
    }

    private void initLike(Info info, ImageView ibLike) {
        if (info.isLike) {
            ibLike.setImageResource(R.drawable.love_selected);
        } else {
            ibLike.setImageResource(R.drawable.love_img);
        }
    }

    @Override
    public int getViewTypeCount() {
        return 3;
    }

    @Override
    public int getItemViewType(int position) {
        int result = 0;
        switch (infos.get(position).getType()) {
            case Info.T:
            case Info.P:
            case Info.TP:
                result = TP;
                break;
            case Info.AP:
                result = AP;
                break;
            case Info.A:
                result = A;
                break;
        }
        return result;
    }

    @Override
    public void onClick(View v) {
        if (v instanceof SquareImageView) {
            squareImageHandle(v);
        } else if (v instanceof ImageView) {
            imageHandle(v);
        } else if (v instanceof Button) {
            buttonHandle(v);
        } else if (v instanceof TextView) {
            textViewHandle(v);
        }
    }

    /**
     * button被点击
     *
     * @param v
     */
    private void buttonHandle(View v) {
        if (v.getTag() != null) {
            Info info = (Info) v.getTag();
            switch (v.getId()) {
                case R.id.ib_info_item_more:
                    showMoreDialog(info);
                    break;
            }
        }
    }

    /**
     * TextView被点击
     *
     * @param v
     */
    private void textViewHandle(View v) {
        if (v.getTag() != null) {
            Info info = (Info) v.getTag();
            switch (v.getId()) {
                case R.id.tv_info_item_dialog_delete_ensure:
                    presenter.deleteInfo((Info) (tvEnsure.getTag()));
                    DialogUtil.hideDialog(deleteDialog);
                    break;
                case R.id.tv_info_item_dialog_delete_cancel:
                    DialogUtil.hideDialog(deleteDialog);
                    break;
                case R.id.tv_info_agree_num:
                case R.id.tv_info_wacth_num:

                    Intent intent = new Intent(context, IntersectionActivity.class);
                    intent.putExtra(IntersectionActivity.INFO_ID, info.getObjectId());
                    context.startActivity(intent);
                    break;
                case R.id.tv_info_item_more_dialog_delete:
                    DialogUtil.hideDialog(moreDeleteDialog);
                    showDeleteDialog(info);
                    break;
                case R.id.tv_info_item_more_dialog_not_want_see:
                    DialogUtil.hideDialog(moreNoWantSeeDialog);
                    presenter.noWantToSee(info);
                    break;
                case R.id.tv_info_item_more_dialog_share:
                    if (moreDeleteDialog != null) {
                        DialogUtil.hideDialog(moreDeleteDialog);
                    } else if (moreNoWantSeeDialog != null) {
                        DialogUtil.hideDialog(moreNoWantSeeDialog);
                    }
                    share(info);
                    break;
                case R.id.tv_info_item_more_dialog_report:
                    DialogUtil.hideDialog(moreNoWantSeeDialog);
                    showRePort(info);
                    break;
                case R.id.tv_info_item_dialog_report_ensure:
                    DialogUtil.hideDialog(reportDialog);
                    presenter.report(info);
                    break;
                case R.id.tv_info_item_dialog_report_cancel:
                    DialogUtil.hideDialog(reportDialog);
                    break;
                case R.id.tv_info_item_nickname:
                case R.id.tv_info_item_time:
                    goLocation(info);
                    break;
            }
        }
    }

    private void goLocation(Info info) {
        Intent intent;
        intent = new Intent(context, ChannelActivity.class);
        intent.putExtra(ChannelActivity.LOCATION_ID, info.getArea().getObjectId());
        intent.putExtra(ChannelActivity.LOCATION_NAME, info.getArea().getLocationName());
        context.startActivity(intent);
    }

    private void share(Info info) {
        Intent intent;
        intent = new Intent(context, SelectContactActivity.class);
        intent.putExtra(ChatActivity.INTENT_ID, ChatActivity.COMMENT);
        intent.putExtra(ChatActivity.NICK_NAME,info.getUser().getUsername());
        intent.putExtra(ChatActivity.INFO_PRAC, info);
        context.startActivity(intent);
    }

    /**
     * ImageVIew被点击
     *
     * @param v
     */
    private void imageHandle(View v) {
        if (v.getTag() != null) {
            Info info = (Info) v.getTag();
            if (v.getTag(R.integer.info_iv_type) != null && v.getTag(R.integer.info_iv_type).equals(iv_type_audio)) {
                playAudioOfA((ImageView) v, info);
            } else if (v.getTag(R.integer.info_iv_type) != null && v.getTag(R.integer.info_iv_type).equals(iv_type_like)) {
                like(v, info);
            } else if (v.getTag(R.integer.info_iv_type) != null && v.getTag(R.integer.info_iv_type).equals(iv_type_chat)) {
                chat(info);
            } else if (v.getTag(R.integer.info_iv_type) != null && v.getTag(R.integer.info_iv_type).equals(iv_type_delete)) {
                showDeleteDialog(info);
            } else if (v.getTag(R.integer.info_iv_type) != null && v.getTag(R.integer.info_iv_type).equals(iv_type_avater)) {
                if (isProFile) {
                    goLocation(info);
                } else {
                    Intent intent = new Intent(context, ProfileActivity.class);
                    intent.putExtra(ProfileActivity.USER_ID, info.getUser().getObjectId());
                    context.startActivity(intent);
                }
            }
        }
    }

    /**
     * 播放A状态下的Audio
     *
     * @param v
     * @param info
     */
    private void playAudioOfA(ImageView v, Info info) {
        switch (info.getType()) {
            case Info.A:
                if (audioPlayer != null && audioPlayer.isPlaying()) {
                    audioPlayer.stop();
                    info.isPlaying = false;
                    changeAudioDrawable(info, v);
                    String path = getAudioPath(info);
                    if (audioPlayer.getPath() != null && !audioPlayer.getPath().equals(path)) {
                        play(info, v, path);
                    }
                } else {
                    String path = getAudioPath(info);
                    play(info, v, path);
                }
                break;
        }
    }

    /**
     * 赞
     *
     * @param v
     * @param info
     */
    private void like(View v, Info info) {
        info.isLike = !info.isLike;
        presenter.requestLike(info, info.isLike);
        if (info.isLike) {
            info.agreeNum = Integer.valueOf(info.agreeNum) + 1 + "";
        } else {
            info.agreeNum = Integer.valueOf(info.agreeNum) - 1 + "";
        }
        if (v.getTag(R.integer.info_base_ivlike_tag_tvagree) != null) {
            ((TextView) (v.getTag(R.integer.info_base_ivlike_tag_tvagree))).setText(info.agreeNum);
        }
        initLike(info, (ImageView) v);
    }

    /**
     * 聊天
     *
     * @param info
     */
    private void chat(Info info) {
        Intent intent;
        intent = new Intent(context, ChatActivity.class);
        intent.putExtra(ChatActivity.INTENT_ID, ChatActivity.COMMENT);
        intent.putExtra(ChatActivity.NICK_NAME,info.getUser().getUsername());
        intent.putExtra(ChatActivity.USER_ID_IN_LONG, info.getUser().getObjectId());
        intent.putExtra(ChatActivity.INFO_PRAC, info);
        context.startActivity(intent);
    }

    /**
     * 方形ImageView被点击
     *
     * @param v
     */
    private void squareImageHandle(View v) {
        if (v.getTag() != null) {
            Info info = (Info) v.getTag();
            switch (info.getType()) {
                case Info.P:
                case Info.TP:
                    Intent intent = new Intent(context, ShowImageActivity.class);
                    if (info.getObjectId()==null){
                        intent.putExtra(ShowImageActivity.IMAGE_URL, "file://"+info.getImagePath());
                    }else {
                        intent.putExtra(ShowImageActivity.IMAGE_URL, info.getImageUrl());
                    }
                    context.startActivity(intent);
                    break;
                case Info.AP:
                    if (audioPlayer != null && audioPlayer.isPlaying()) {
                        audioPlayer.stop();
                        info.isPlaying = false;
                        changeAudioDrawable(info, (ImageView) v.getTag(R.integer.info_ap_iv_audio));
                        String path = getAudioPath(info);
                        if (audioPlayer.getPath() != null && !audioPlayer.getPath().equals(path)) {
                            play(info, (ImageView) v.getTag(R.integer.info_ap_iv_audio), path);
                        }
                    } else {
                        String path = getAudioPath(info);
                        play(info, (ImageView) v.getTag(R.integer.info_ap_iv_audio), path);
                    }
                    break;
            }
        }
    }

    @Override
    protected InfoBasePresenter createP() {
        return new InfoBasePresenter();
    }

    @Override
    public void showReadedUserCount(Base base, Info info) {
        if (((Info) base.tvWatchNum.getTag()).getObjectId().equals(info.getObjectId())) {
            base.tvWatchNum.setText(info.watchNum);
            base.tvWatchNum.setOnClickListener(InfoAdapter.this);
        }
    }

    @Override
    public void showAgreeUserCount(Base base, Info info) {
        if (((Info) base.tvAgreeNum.getTag()).getObjectId().equals(info.getObjectId())) {
            base.tvAgreeNum.setText(info.agreeNum);
            base.tvAgreeNum.setOnClickListener(InfoAdapter.this);
        }
    }

    @Override
    public void setIsLike(Base base, Info info) {
        if (((Info) base.ibLike.getTag()).getObjectId().equals(info.getObjectId())) {
            base.ibLike.setTag(R.integer.info_iv_type, iv_type_like);
            base.ibLike.setTag(R.integer.info_base_ivlike_tag_tvagree, base.tvAgreeNum);
            base.ibLike.setOnClickListener(InfoAdapter.this);
            initLike(info, base.ibLike);
        }
    }

    @Override
    public void deleteSucess(Info info) {
        infos.remove(info);
        notifyDataSetChanged();
    }

    @Override
    public void deleteError(String message) {
        ToastUtil.showCustomToastInRelease(App.application, message);
    }

    @Override
    public void noWantToSeeSucess(Info info) {
        infos.remove(info);
        notifyDataSetChanged();
    }

    @Override
    public void showReportError(String message) {
        ToastUtil.showCustomToastInRelease(context, message);
    }

    @Override
    public void showReportSucess() {
        ToastUtil.showCustomToastInRelease(context, context.getString(R.string.info_report_sucess));
    }

    private void play(final Info info, final ImageView ivAudio, final String audioPath) {
        if (!TextUtils.isEmpty(audioPath) && new File(audioPath).exists()) {
            try {
                audioPlayer = new AudioPlayer();
                info.isPlaying = true;
                changeAudioDrawable(info, ivAudio);
                if (info.getType() == Info.A) {
                    TextView tvProgress = (TextView) ivAudio.getTag(R.integer.info_a_tv_progress);
                    if (tvProgress.getTag(R.integer.info_a_tv_progress_first_width) == null) {
                        tvProgress.setTag(R.integer.info_a_tv_progress_first_width, tvProgress.getMeasuredWidth());
                    }
                    sendMessage(info, tvProgress);
                }
                audioPlayer.start(audioPath, new AudioPlayer.OnFinishedCallBack() {
                    @Override
                    public void onFinished(String path) {
                        info.isPlaying = false;
                        changeAudioDrawable(info, ivAudio);
                        if (info.getType() == Info.A) {
                            info.marginRight = 0;
                            changeMargin(info, (TextView) ivAudio.getTag(R.integer.info_a_tv_progress));
                        }
                    }
                });
            } catch (IOException | IllegalArgumentException e) {
                e.printStackTrace();
                info.isPlaying = false;
                audioPlayer.stop();
                changeAudioDrawable(info, ivAudio);
            }
        }
    }

    /**
     * 发送消息，及时更新播放音频的时间
     *
     * @param info
     * @param tvProgress
     */
    private void sendMessage(Info info, TextView tvProgress) {
        Message message = Message.obtain();
        AudioProgress audioProgress = new AudioProgress();
        audioProgress.info = info;
        audioProgress.tvProgress = tvProgress;
        message.obj = audioProgress;
        message.what = SHOW_PROGRESS;
        handler.sendMessage(message);
    }

    private void changeAudioDrawable(Info info, ImageView ivAudio) {
        if (ivAudio.getTag() == info) {
            ivAudio.setSelected(info.isPlaying);
        }
    }

    public void changeList(List<Info> infos) {
        this.infos = infos;
        notifyDataSetChanged();
    }

    /**
     * 显示删除确认对话框
     */
    private void showDeleteDialog(Info info) {
        if (deleteDialog == null) {
            deleteDialog = DialogUtil.showDiaLog(R.layout.dialog_info_item_delete_is_ok, context);
            tvEnsure = (TextView) deleteDialog.findViewById(R.id.tv_info_item_dialog_delete_ensure);
            tvEnsure.setOnClickListener(this);
            tvCancel = (TextView) deleteDialog.findViewById(R.id.tv_info_item_dialog_delete_cancel);
            tvCancel.setOnClickListener(this);
        } else {
            deleteDialog.show();
        }
        tvEnsure.setTag(info);
        tvCancel.setTag(info);
    }

    public void showMoreDialog(Info info) {
        if (info.getUser().getObjectId().equals(User.getCurrentUser().getObjectId())) {
            if (moreDeleteDialog == null) {
                //自己发的内容
                moreDeleteDialog = DialogUtil.showDiaLog(R.layout.dialog_info_item_more_delete, context);
                tvDelete = (TextView) moreDeleteDialog.findViewById(R.id.tv_info_item_more_dialog_delete);
                tvShareMine = (TextView) moreDeleteDialog.findViewById(R.id.tv_info_item_more_dialog_share);
                tvShareMine.setOnClickListener(this);
                tvDelete.setOnClickListener(this);
            } else {
                moreDeleteDialog.show();
            }
            tvDelete.setTag(info);
            tvShareMine.setTag(info);
        } else {
            if (moreNoWantSeeDialog == null) {
                moreNoWantSeeDialog = DialogUtil.showDiaLog(R.layout.dialog_info_item_more, context);
                tvNotWantSee = (TextView) moreNoWantSeeDialog.findViewById(R.id.tv_info_item_more_dialog_not_want_see);
                tvOtherShare = (TextView) moreNoWantSeeDialog.findViewById(R.id.tv_info_item_more_dialog_share);
                tvOtherReport = (TextView) moreNoWantSeeDialog.findViewById(R.id.tv_info_item_more_dialog_report);
                tvOtherShare.setOnClickListener(this);
                tvNotWantSee.setOnClickListener(this);
                tvOtherReport.setOnClickListener(this);
            } else {
                moreNoWantSeeDialog.show();
            }
            tvNotWantSee.setTag(info);
            tvOtherShare.setTag(info);
            tvOtherReport.setTag(info);
        }
    }

    public void showRePort(Info info) {
        if (reportDialog == null) {
            //自己发的内容
            reportDialog = DialogUtil.showDiaLog(R.layout.dialog_report_info, context);
            tvReortEnsure = (TextView) reportDialog.findViewById(R.id.tv_info_item_dialog_report_ensure);
            tvReportCancel = (TextView) reportDialog.findViewById(R.id.tv_info_item_dialog_report_cancel);
            tvReortEnsure.setOnClickListener(this);
            tvReportCancel.setOnClickListener(this);
        } else {
            reportDialog.show();
        }
        tvReortEnsure.setTag(info);
        tvReportCancel.setTag(info);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        context = null;
        infos = null;
        moreDeleteDialog = null;
        moreNoWantSeeDialog = null;
        tvNotWantSee = null;
        tvDelete = null;
        tvOtherShare = null;
        tvShareMine = null;
        reportDialog = null;
        tvReportCancel = null;
        tvReortEnsure = null;
        tvOtherReport = null;
    }

    class AudioProgress {
        private TextView tvProgress;
        private Info info;
    }

    public class TP {
        private Base base = new Base();

        @FindView(R.id.view_item_info_square)
        private SquareImageView viewPic;

        @CustomFont(ConsTants.TYPE_FACE)
        @FindView(R.id.tv_info_item_text)
        private TextView tvText;
        @FindView(R.id.tv_info_item_tp_divider)
        private TextView tvDivider;
    }

    public class AP {
        private Base base = new Base();

        @FindView(R.id.view_item_info_square)
        private SquareImageView viewPic;

        @FindView(R.id.iv_info_item_ap_audio)
        private ImageView ivAudio;
        @CustomFont(ConsTants.TYPE_FACE)
        @FindView(R.id.tv_info_item_text)
        private TextView tvText;
    }

    public class A {
        private Base base = new Base();

        @FindView(R.id.iv_info_item_a_audio)
        private ImageView ivAudio;

        @CustomFont(ConsTants.TYPE_FACE)
        @FindView(R.id.tv_info_item_a_progress)
        private TextView tvProgress;
    }

    public class Base {
        @FindView(R.id.view_rounded_image_ifo_item_avater)
        private SimpleDraweeView roundedImageView;

        @CustomFont(ConsTants.TYPE_FACE)
        @FindView(R.id.tv_info_item_nickname)
        private TextView tvNickName;

        @CustomFont(ConsTants.TYPE_FACE)
        @FindView(R.id.tv_info_item_time)
        private TextView tvTime;

        @CustomFont(ConsTants.TYPE_FACE)
        @FindView(R.id.ib_info_item_more)
        private Button ibMore;

        @CustomFont(ConsTants.TYPE_FACE)
        @FindView(R.id.tv_info_agree_num)
        private TextView tvAgreeNum;

        @CustomFont(ConsTants.TYPE_FACE)
        @FindView(R.id.tv_info_wacth_num)
        private TextView tvWatchNum;

        @FindView(R.id.ib_info_item_like_bg)
        private ImageView ibLike;

        @FindView(R.id.ib_info_item_chat_bg)
        private ImageView ibChat;

        @FindView(R.id.ib_info_item_self_delete_bg)
        private ImageView ibDelete;

    }
}
